package edu.fmi.st.epf.types.projectmember.ui.pages;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.emf.edit.ui.provider.AdapterFactoryContentProvider;
import org.eclipse.epf.authoring.ui.AuthoringUIPlugin;
import org.eclipse.epf.authoring.ui.AuthoringUIResources;
import org.eclipse.epf.authoring.ui.forms.AssociationFormPage;
import org.eclipse.epf.library.edit.TngAdapterFactory;
import org.eclipse.epf.library.edit.command.IActionManager;
import org.eclipse.epf.library.edit.util.MethodElementUtil;
import org.eclipse.epf.uma.Checklist;
import org.eclipse.epf.uma.Concept;
import org.eclipse.epf.uma.EstimationConsiderations;
import org.eclipse.epf.uma.Example;
import org.eclipse.epf.uma.Guidance;
import org.eclipse.epf.uma.Guideline;
import org.eclipse.epf.uma.Practice;
import org.eclipse.epf.uma.Report;
import org.eclipse.epf.uma.ReusableAsset;
import org.eclipse.epf.uma.SupportingMaterial;
import org.eclipse.epf.uma.Task;
import org.eclipse.epf.uma.Template;
import org.eclipse.epf.uma.Tool;
import org.eclipse.epf.uma.ToolMentor;
import org.eclipse.epf.uma.UmaPackage;
import org.eclipse.epf.uma.WorkProduct;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.forms.editor.FormEditor;

public class TestPage extends AssociationFormPage {
	
	Object[] STATIC_SKILLS = {"TEAM WORKER", null, "BLA pgjdrop pdogropj", null};

	public TestPage(FormEditor editor, String id, String name) {
		super(editor, id, name);
		// TODO Auto-generated constructor stub
	}
	public void init(IEditorSite site, IEditorInput input) {
		super.init(site, input);
		setUseCategory2(false);
		setUseCategory3(false);
	}
	
	/**
	 * @see org.eclipse.epf.authoring.ui.forms.AssociationFormPage#addItemsToModel1(ArrayList)
	 */
	protected void addItemsToModel1(ArrayList addItems) {
		// Update the model.
		IActionManager actionMgr = editor.getActionManager();
		if (!addItems.isEmpty()) {
			for (Iterator it = addItems.iterator(); it.hasNext();) {
				Guidance item = (Guidance) it.next();

				// Consolidating the separate form page code
				// to accomodate better filtering.
				if (contentElement instanceof Task) {
//					if (item instanceof Estimate) {
//						editor.getActionManager().doAction(IActionManager.SET,
//								contentElement,
//								UmaPackage.eINSTANCE.getTask_Estimate(),
//								(Estimate) item, -1);
//					} else 
					if (item instanceof ToolMentor) {
						actionMgr.doAction(IActionManager.ADD,
								contentElement,
								UmaPackage.eINSTANCE.getTask_ToolMentors(),
								(ToolMentor) item, -1);
						continue;
					} else if (item instanceof EstimationConsiderations) {
						actionMgr.doAction(IActionManager.ADD, contentElement,
								UmaPackage.eINSTANCE.getTask_EstimationConsiderations(),
								item, -1);
						continue;
					}
				}
				// In case of WorkProduct, WorkProduct is associated to ToolMentor,
				// Estimate, Report and Template guidance
				if (contentElement instanceof WorkProduct) {
					if (item instanceof Template) {
						actionMgr
								.doAction(IActionManager.ADD, contentElement,
										UmaPackage.eINSTANCE
												.getWorkProduct_Templates(),
										item, -1);
						continue;
					} else if (item instanceof ToolMentor) {
						actionMgr
								.doAction(IActionManager.ADD, contentElement,
										UmaPackage.eINSTANCE
												.getWorkProduct_ToolMentors(),
										item, -1);
						continue;
					} else if (item instanceof Report) {
						actionMgr.doAction(IActionManager.ADD, contentElement,
								UmaPackage.eINSTANCE.getWorkProduct_Reports(),
								item, -1);
						continue;
					} 
//					else if (item instanceof Estimate) {
//						actionMgr.doAction(IActionManager.SET, contentElement,
//								UmaPackage.eINSTANCE.getWorkProduct_Estimate(),
//								item, -1);
//					}
					else if (item instanceof EstimationConsiderations) {
						actionMgr.doAction(IActionManager.ADD, contentElement,
								UmaPackage.eINSTANCE.getWorkProduct_EstimationConsiderations(),
								item, -1);
						continue;
					}
				}

				// Guidance for content element.
				if (item instanceof Checklist) {
					editor.getActionManager()
							.doAction(
									IActionManager.ADD,
									contentElement,
									UmaPackage.eINSTANCE
											.getContentElement_Checklists(),
									(Checklist) item, -1);
					continue;
				} else if (item instanceof Concept) {
					editor.getActionManager().doAction(
							IActionManager.ADD,
							contentElement,
							UmaPackage.eINSTANCE
									.getContentElement_ConceptsAndPapers(),
							(Concept) item, -1);
					continue;
				} else if (item instanceof Example) {
					editor.getActionManager().doAction(IActionManager.ADD,
							contentElement,
							UmaPackage.eINSTANCE.getContentElement_Examples(),
							(Example) item, -1);
					continue;
				} else if (item instanceof SupportingMaterial) {
					editor.getActionManager().doAction(
							IActionManager.ADD,
							contentElement,
							UmaPackage.eINSTANCE
									.getContentElement_SupportingMaterials(),
							(SupportingMaterial) item, -1);
					continue;
				} else if (item instanceof Guideline) {
					editor.getActionManager()
							.doAction(
									IActionManager.ADD,
									contentElement,
									UmaPackage.eINSTANCE
											.getContentElement_Guidelines(),
									(Guideline) item, -1);
					continue;
				} else if (item instanceof ReusableAsset) {
					editor.getActionManager().doAction(IActionManager.ADD,
							contentElement,
							UmaPackage.eINSTANCE.getContentElement_Assets(),
							(ReusableAsset) item, -1);
					continue;
				} else {
					AuthoringUIPlugin
							.getDefault()
							.getLogger()
							.logError(
									"Unable to add guidance " + item.getType().getName() + ":" + item.getName() + //$NON-NLS-1$ //$NON-NLS-2$
											"to " //$NON-NLS-1$
											+ contentElement.getType()
													.getName()
											+ ":" + contentElement.getName()); //$NON-NLS-1$ 
				}
			}
//			setDirty(true);
		}
	}

	/**
	 * @see org.eclipse.epf.authoring.ui.forms.AssociationFormPage#removeItemsFromModel1(ArrayList)
	 */
	protected void removeItemsFromModel1(ArrayList rmItems) {
		// Update the model.
		IActionManager actionMgr = editor.getActionManager();
		if (!rmItems.isEmpty()) {
			for (Iterator it = rmItems.iterator(); it.hasNext();) {
				Guidance item = (Guidance) it.next();

				// In case of Task, Task is associated to ToolMentor and
				// Estimate guidance
				if (contentElement instanceof Task) {
					if (item instanceof ToolMentor) {
						editor.getActionManager().doAction(
								IActionManager.REMOVE, contentElement,
								UmaPackage.eINSTANCE.getTask_ToolMentors(),
								item, -1);
						continue;
					} 
//					else if (item instanceof Estimate) {
//						editor.getActionManager().doAction(IActionManager.SET,
//								contentElement,
//								UmaPackage.eINSTANCE.getTask_Estimate(), null,
//								-1);
//					}
					else if (item instanceof EstimationConsiderations) {
						editor.getActionManager().doAction(IActionManager.REMOVE,
								contentElement,
								UmaPackage.eINSTANCE.getTask_EstimationConsiderations(), item,
								-1);
						continue;
					}
				}
				// In case of WorkProduct, WorkProduct is associated to ToolMentor,
				// Estimate, Report and Template guidance
				if (contentElement instanceof WorkProduct) {
					if (item instanceof Report) {
						actionMgr.doAction(IActionManager.REMOVE,
								contentElement, UmaPackage.eINSTANCE
										.getWorkProduct_Reports(), item, -1);
						continue;
					} else if (item instanceof Template) {
						actionMgr.doAction(IActionManager.REMOVE,
								contentElement, UmaPackage.eINSTANCE
										.getWorkProduct_Templates(), item, -1);
						continue;
					} else if (item instanceof ToolMentor) {
						actionMgr
								.doAction(IActionManager.REMOVE,
										contentElement, UmaPackage.eINSTANCE
												.getWorkProduct_ToolMentors(),
										item, -1);
						continue;
					} 
//					else if (item instanceof Estimate) {
//						actionMgr.doAction(IActionManager.SET, contentElement,
//								UmaPackage.eINSTANCE.getWorkProduct_Estimate(),
//								null, -1);
//					}
					else if (item instanceof EstimationConsiderations) {
						editor.getActionManager().doAction(IActionManager.REMOVE,
								contentElement,
								UmaPackage.eINSTANCE.getWorkProduct_EstimationConsiderations(), item,
								-1);
						continue;
					}
				}
				// Guidance for the method element.
				if (item instanceof Checklist) {
					editor.getActionManager()
							.doAction(
									IActionManager.REMOVE,
									contentElement,
									UmaPackage.eINSTANCE
											.getContentElement_Checklists(),
									item, -1);
					continue;
				} else if (item instanceof Concept) {
					editor.getActionManager().doAction(
							IActionManager.REMOVE,
							contentElement,
							UmaPackage.eINSTANCE
									.getContentElement_ConceptsAndPapers(),
							(Concept) item, -1);
					continue;
				} else if (item instanceof Example) {
					editor.getActionManager().doAction(IActionManager.REMOVE,
							contentElement,
							UmaPackage.eINSTANCE.getContentElement_Examples(),
							(Example) item, -1);
					continue;
				} else if (item instanceof SupportingMaterial) {
					editor.getActionManager().doAction(
							IActionManager.REMOVE,
							contentElement,
							UmaPackage.eINSTANCE
									.getContentElement_SupportingMaterials(),
							(SupportingMaterial) item, -1);
					continue;
				} else if (item instanceof Guideline) {
					editor.getActionManager()
							.doAction(
									IActionManager.REMOVE,
									contentElement,
									UmaPackage.eINSTANCE
											.getContentElement_Guidelines(),
									(Guideline) item, -1);
					continue;
				} else if (item instanceof ReusableAsset) {
					editor.getActionManager().doAction(IActionManager.REMOVE,
							contentElement,
							UmaPackage.eINSTANCE.getContentElement_Assets(),
							(ReusableAsset) item, -1);
					continue;
				} else {
					AuthoringUIPlugin
							.getDefault()
							.getLogger()
							.logError(
									"Unable to remove guidance " + item.getType().getName() + ":" + item.getName() + //$NON-NLS-1$ //$NON-NLS-2$
											"from " //$NON-NLS-1$
											+ contentElement.getType()
													.getName()
											+ ":" + contentElement.getName()); //$NON-NLS-1$ 
				}
			}
//			setDirty(true);
		}
	}

	@Override
	protected String getSectionDescription() {
		return "ssssssssssskills";
	}
	
	protected String getSectionName() {
		return "getSectionName";
	}

	/**
	 * @see org.eclipse.epf.authoring.ui.forms.AssociationFormPage#getSelectedLabel()
	 */
	protected String getSelectedLabel() {
		return "getSelectedLabel";
	}
	
	/**
	 * @see org.eclipse.epf.authoring.ui.forms.AssociationFormPage#getSelectedLabel2()
	 */
	protected String getSelectedLabel2() {
		return "Label2";
	}
	
	/**
	 * @see org.eclipse.epf.authoring.ui.forms.AssociationFormPage#getSelectedLabel3()
	 */
	protected String getSelectedLabel3() {
		return "Label3";
	}
	
	protected void initContentProviderSelected() {
		contentProviderSelected = new AdapterFactoryContentProvider(
				TngAdapterFactory.INSTANCE
						.getNavigatorView_ComposedAdapterFactory()) {
			public Object[] getElements(Object object) {
				STATIC_SKILLS[1] = object;
				STATIC_SKILLS[3] = object;
				return STATIC_SKILLS;
			}
		};
		viewer_selected.setContentProvider(contentProviderSelected);
	}
}
